package view;

import java.util.Observer;
import java.util.Observable;
import java.util.List;
import java.util.Iterator;

import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import org.jdom.Element;

public class MenuSelectionSection extends JPanel implements Observer{
	
	private static final long serialVersionUID = 1L;
	
	public MenuSelectionSection(Observable obs){
		obs.addObserver(this);
		
        setBounds(160, 5, 410, 300);
        setLayout(null);
	}
	
	public void update(Observable obs, Object obj){
		
    	List<Element> menuElements = (List<Element>)obj;
    	Iterator<Element> menuElementsIt = menuElements.iterator();
    	removeAll();
        
    	ButtonGroup group = new ButtonGroup();
    	while(menuElementsIt.hasNext()){
    		Element element = menuElementsIt.next();
    		
    		JRadioButton elementSelector = new JRadioButton(element.getChildText("name"));
    		elementSelector.setBounds(0, 10 + menuElements.indexOf(element)*25, 285, 20);
    		elementSelector.setName(element.getChildText("price"));
    		elementSelector.setSelected((menuElements.indexOf(element) == 0) ? true: false);
    		group.add(elementSelector);       
    		add(elementSelector);
    		
    		JLabel elementPrice = new JLabel(element.getChildText("price")+"$");
    		elementPrice.setBounds(350, 10 + menuElements.indexOf(element)*25, 50, 20);
    		add(elementPrice);
    	}
    	
    	repaint();
	}
}
